﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using Bll;
using System.Data;
using System.IO;

namespace Accounting.admin.Download
{
    public partial class DownloadRevise : System.Web.UI.Page
    {
        BllPublic b = new BllPublic();
        ModelPublic m = new ModelPublic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Name"] != null && Session["Type"] != null)
                {
                    UserType.Text = Session["Type"].ToString();
                    if (UserType.Text == "0")
                    {
                        Release.Visible = true;
                    }
                    else
                    {
                        Release.Visible = false;
                    }
                    if (Request.QueryString["Id"] != null)
                    {
                        m.Id = Request.QueryString["Id"].ToString();
                        lblId.Text= Request.QueryString["Id"].ToString();
                        SelectBind();
                        ContentBind();

                    }
                }
                else
                {
                    ASubmit.Disabled = false;
                    btnReturn.Enabled = false;
                }

                
            }
        }
        public void SelectBind()
        {
            m.NavId = "9";
            DataTable dt = b.SubNav_Inquire_All(m);
            foreach (DataRow my in dt.Rows)
            {
                string name = "--" + my["Name"].ToString() + "--";
                string id = my["Id"].ToString();
                NavSubList.Items.Add(new ListItem(name, id));
            }

            DataTable nav = b.Nav_Inquire_All();

            foreach (DataRow my in nav.Rows)
            {
                if (my["Id"].ToString() == "9")
                {
                    H2_Title.InnerText = "修改：" + my["Name"].ToString();
                }
            }
        }

        public void ContentBind()
        {
            m.Id = lblId.Text;
            DataTable dt = b.Information_Inquire_Single(m);

            foreach (DataRow my in dt.Rows)
            {
                NavSubList.SelectedValue = my["NavSubId"].ToString();
                txtTitle.Text = my["Title"].ToString();
                txtAuthor.Text = my["Author"].ToString();
                txtNumber.Text = my["Number"].ToString();
                txtDateTime.Text = my["DateTime"].ToString();
                Content.Text = my["_Content"].ToString();
                lblFileName.Text = my["FileName"].ToString();
                lblFileRoute.Text = my["FileRoute"].ToString();
                
                if (my["Release"].ToString() == "True")
                    Release.Checked = true;
                else
                    Release.Checked = false;

                if (my["Recommend"].ToString()== "True")
                    Recommend.Checked = true;
                else
                    Recommend.Checked = false;

                if(my["RecommendRoute"].ToString()!="NULL")
                {
                    Recommend_Div.Style.Value = "display:block";
                    RecommendFile_Show.ImageUrl = "../../"+ my["RecommendRoute"].ToString();
                }

                lblRecommendName.Text = my["RecommendName"].ToString();
                lblRecommendRoute.Text = my["RecommendRoute"].ToString();

                if (my["Recommend"].ToString() == "True")
                    Recommend_Div.Style["display"] = "block";
                else
                    Recommend_Div.Style["display"] = "none";
            }
        }

        protected void btnRevise_Click(object sender, EventArgs e)
        {
            string folderRouteImg = "";
            string folderRoutefile = "";

            m.Title = "'" + txtTitle.Text.Trim() + "'";
            m.DateTime = "'" + txtDateTime.Text.Trim() + "'";
            m.Author = "'" + txtAuthor.Text.Trim() + "'";
            m.NavSubId = "'" + NavSubList.SelectedValue + "'";
            m.Content = Content.Text;
            m.Number = "'" + txtNumber.Text.Trim() + "'";

            m.Id = "'" + lblId.Text + "'";

            if (Recommend.Checked == true)
            {
                m.Recommend = "'True'";

                if (RecommendFile.HasFile)
                {
                    //推荐图片上传
                    string Upname = RecommendFile.FileName;

                    int Pointposition = 0;
                    string FileName = "";
                    string suffix = "";

                    for (int i = 0; i < Upname.Length; i++)
                    {
                        if (Upname[i] == '.')
                        {
                            Pointposition = i;
                            break;
                        }
                    }

                    for (int i = 0; i < Pointposition; i++)
                    {
                        FileName += Upname[i];
                    }

                    for (int i = Pointposition; i < Upname.Length; i++)
                    {
                        suffix += Upname[i];
                    }

                    m.NavId = "9";
                    m.DateTime = txtDateTime.Text.Trim();

                    int num = b.Information_Count_Img_Number(m);

                    string Time = System.DateTime.Now.ToString("yyyyMMdd");
                    folderRouteImg = "images/Recommend/" + Time +9+ (num + 1).ToString() + suffix;

                    m.RecommendRoute ="'"+ folderRouteImg+"'";
                    m.RecommendName ="'"+ RecommendFile.FileName+"'";
                }
                else
                {
                    m.RecommendRoute = "'" + lblRecommendRoute.Text+"'";
                    m.RecommendName = "'" + lblRecommendName.Text+"'";
                }
            }
            else
            {
                m.Recommend = "'False'";
                if (lblRecommendName.Text == "")
                    m.RecommendName = "NULL";
                else
                    m.RecommendName = "'" + lblRecommendName.Text+"'";
                if (lblRecommendRoute.Text == "")
                    m.RecommendRoute = "NULL";
                else
                    m.RecommendRoute = "'" + lblRecommendRoute.Text+"'";
            }

            if (UserType.Text == "0")
            {
                if (Release.Checked == true)
                    m.Release = "'True'";
                else
                    m.Release = "'False'";
            }
            else
            {
                m.Release = "'false'";
            }

            if (ReleaseFile.HasFile)
            {
                string Upname = ReleaseFile.FileName;

                int Pointposition = 0;
                string FileName = "";
                string suffix = "";

                for (int i = 0; i < Upname.Length; i++)
                {
                    if (Upname[i] == '.')
                    {
                        Pointposition = i;
                        break;
                    }
                }

                for (int i = 0; i < Pointposition; i++)
                {
                    FileName += Upname[i];
                }

                for (int i = Pointposition; i < Upname.Length; i++)
                {
                    suffix += Upname[i];
                }

                m.NavId = "9";
                m.DateTime = txtDateTime.Text.Trim();

                int num = b.Information_Count_File_Number(m);

                string Time = System.DateTime.Now.ToString("yyyyMMdd");
                folderRoutefile = "InformationFile/" + Time +9+ (num + 1).ToString() + suffix;

                m.FileName = "'"+ ReleaseFile.FileName+"'";
                m.FileRoute ="'"+ folderRoutefile+"'";
            }
            else
            {
                if (lblFileName.Text == "")
                    m.FileName = "NULL";
                else
                    m.FileName = "'" + lblFileName.Text+"'";
                if (lblFileRoute.Text == "")
                    m.FileRoute = "NULL";
                else
                    m.FileRoute = "'" + lblFileRoute.Text+"'";
            }

            string message = b.Information_Modify(m);


            if (message=="1")
            {
                Response.Write("<script>alert('修改成功！')</script>");
                
                //推荐图片上传
                if (m.RecommendName!="NULL" && m.RecommendName!="'"+lblRecommendName.Text+"'")
                {
                    try
                    {
                        RecommendFile.SaveAs(Server.MapPath("../../" + folderRouteImg));
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script>alert('上传失败:" + ex.Message + "')</script> ");
                    }
                }

                //文件上传
                if(m.FileName!="NULL"&&m.FileName!="'"+lblFileName.Text+"'")
                {
                    try
                    {
                        ReleaseFile.SaveAs(Server.MapPath("../../" + folderRoutefile));
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script>alert('上传失败:" + ex.Message + "')</script> ");
                    }
                }

                ContentBind();
            }
            else
            {
                Response.Write("<script>alert('修改失败')</script>");
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["type"] != null)
            {
                if (Request.QueryString["type"].ToString() == "0")
                    Response.Redirect("./DownloadAdmin.aspx");
                else
                    Response.Redirect("./DownloadUnpublished.aspx");
            }
        }
        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            if (lblFileRoute.Text != "")
            {
                string fileroute = lblFileRoute.Text;
                int routeNum = 0;
                for (int i = 0; i < fileroute.Length; i++)
                {
                    if (fileroute[i].ToString() == ".")
                    {
                        routeNum = i;
                    }
                }
                string Suffix = "";
                for (int i = routeNum; i < fileroute.Length; i++)
                {
                    Suffix += fileroute[i];

                }

                string filename = lblFileName.Text + Suffix;

                string path = Server.MapPath("../../" + lblFileRoute.Text);
                FileInfo fi = new FileInfo(path);
                if (fi.Exists)
                {
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));
                    Response.AddHeader("Content-Length", fi.Length.ToString());
                    Response.ContentType = "application/octet-stream;charset=gb2321";
                    Response.WriteFile(fi.FullName);
                    Response.Flush();
                    Response.Close();
                }
            }
            else
                Response.Write("<script>alert('没有原文件！')</script>");


        }
    }
}